﻿using RimWorld;
using System.Collections.Generic;
using System;
using Verse;
using Verse.AI;

namespace RimZombieGirl
{
    /// <summary>
    /// 类似于 JobGiver_Haul，用于动物自动执行清洁任务。
    /// </summary>
    public class JobGiver_Cleaning : ThinkNode_JobGiver
    {
        protected override Job TryGiveJob(Pawn pawn)
        {
            Predicate<Thing> predicate = (Thing f) => f.def.category == ThingCategory.Filth;
            Thing thing = null;
            if (pawn != null && pawn.Map != null)
            {
                List<Thing> filthInHomeArea = pawn.Map.listerFilthInHomeArea.FilthInHomeArea;
                if (filthInHomeArea != null && filthInHomeArea.Count > 0)
                {
                    for (int i = 0; i < filthInHomeArea.Count; i++)
                    {
                        if (!filthInHomeArea[i].Position.IsForbidden(pawn) && pawn.CanReserve(filthInHomeArea[i], 1, -1, ReservationLayerDefOf.Floor) && pawn.CanReach(filthInHomeArea[i], PathEndMode.Touch, Danger.Deadly))
                        {
                            Thing thing2 = filthInHomeArea[i];
                            if (thing != null && thing2 != null && (thing2.Position - pawn.Position).LengthHorizontal < (thing.Position - pawn.Position).LengthHorizontal)
                            {
                                thing = thing2;
                            }
                            else if (thing == null)
                            {
                                thing = thing2;
                            }
                        }
                    }
                }
                if (thing != null && pawn.CanReserve(thing))
                {
                    Job job = new Job(JobDefOf.Clean);
                    job.AddQueuedTarget(TargetIndex.A, thing);
                    return job;
                }
            }
            return null;
        }
    }
}